package com.farmpost.server.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farmpost.server.pojo.Comment;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.service.ICommentService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author king
 * @since 2022-04-08
 */
@RestController
@Slf4j
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService iCommentService;

    @PreAuthorize("hasAnyRole('admin,certificated,has_login,test')")
    @ApiOperation(value = "发表评论")
    @PostMapping("/postComment")
    public RespBean postComment(@RequestBody String content) {
        return iCommentService.postComment(content);
    }

    @ApiOperation(value = "按时间查询最新评论")
    @GetMapping("/getCommentByTime")
    public RespBean getCommentByTime(@RequestParam long currentPage) {
        Page<Comment> page = new Page<>(currentPage,20,false);
        log.info("getCommentByTime入参：{}",  page);
        return iCommentService.getCommentByTime(page);
    }

    @ApiOperation(value = "分页查询评论")
    @GetMapping("/queryCommentByPage")
    public RespBean queryComment(Integer pageIndex) {
        return iCommentService.queryComment(pageIndex);
    }

    @ApiOperation(value = "根据userId查询评论")
    @GetMapping("/getCommentById")
    public List<Comment> getCommentById(Integer userId) {
        return iCommentService.getCommentById(userId);
    }

    @ApiOperation(value = "点赞评论")
    @PostMapping("/updateGreatCount")
    public RespBean updateGreatCount(@RequestBody Integer topicId) {
        return iCommentService.updateGreatCount(topicId);
    }
}
